package com.mobilesales.android.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import com.mobilesales.android.R;
import com.mobilesales.android.model.clients.Client;
import com.mobilesales.android.model.orders.Order;
import com.mobilesales.android.model.orders.OrderItem;
import com.mobilesales.android.util.CustomFilter;

public class OrderAdapter extends ArrayAdapter<Order> {

	List<Order> orders;
	List<Client> clients;

	public OrderAdapter(Context context, int textViewResourceId,
			List<Order> objects, List<Client> clients) {
		super(context, textViewResourceId, objects);
		this.orders = new ArrayList<Order>(objects);
		this.clients = clients;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					"layout_inflater");
			v = vi.inflate(R.layout.order_list_item, null);
		}

		Order order = null;
		if ((position >= 0) && (position < this.getCount())) {
			order = this.getItem(position);
		}

		if (order != null) {
			TextView clientNameTxt = (TextView) v
					.findViewById(R.id.clientNameTxt);
			TextView clientAddressTxt = (TextView) v
					.findViewById(R.id.clientAddressTxt);
			TextView dateTxt = (TextView) v.findViewById(R.id.dateTxt);
			TextView statusTxt = (TextView) v.findViewById(R.id.statusTxt);
			TextView totalPriceTxt = (TextView) v
					.findViewById(R.id.totalPriceTxt);

			Client cli = this.getClient(order.ClientId);
			Double totalPrice = this.countTotalPrice(order.Items, cli);
			clientNameTxt.setText(cli.Name);
			
			clientAddressTxt.setText(String.format("%s\n%s, %s", cli.Street,
					cli.Zipcode, cli.City));
			dateTxt.setText(order.Date.split(" ")[0]);
			statusTxt.setText(order.Status.Name);
			if (totalPrice != null)
				totalPriceTxt.setText(String.format("%1$,.2f", totalPrice));
			else
				totalPriceTxt.setText("---");
		}
		return v;
	}

	private Client getClient(int id) {
		if (clients != null) {
			for (Client c : clients) {
				if (c.Id == id)
					return c;
			}
		}
		return null;
	}

	private Double countTotalPrice(List<OrderItem> items, Client client) {
		if (items != null) {
			double price = 0;
			for (OrderItem i : items) {
				if (i.Product != null) {
					price += i.Quantity * i.Product.Price
							* (1 + i.Product.Spread)
							* (1 + i.Product.Tax.Value);
				}
			}
			if (client != null && client.Group != null)
				price *= (1 - client.Group.Discount);
			return price;
		}
		return null;

	}

	@Override
	public Filter getFilter() {
		return new CustomFilter<Order>(this, orders) {
			@Override
			protected boolean fits(String query, Order order) {
				String lcQuery = query.toLowerCase();
				Client cli = getClient(order.ClientId);

				if (order.Date.toLowerCase().contains(lcQuery)
						|| cli.Name.toLowerCase().contains(lcQuery)
						|| cli.Street.toLowerCase().contains(lcQuery)
						|| cli.City.toLowerCase().contains(lcQuery)
						|| cli.Zipcode.toLowerCase().contains(lcQuery)) {
					return true;
				}
				return false;
			}
		};
	}

}
